package service

import (
	"fitness/go-admin/internal/model"
	"fitness/go-admin/internal/repository"
)

// MenuService 菜单服务接口
type MenuService interface {
	CreateMenu(req *model.Menu) error
	GetMenuByID(id uint) (*model.Menu, error)
	GetMenuList() ([]model.MenuTree, error)
	UpdateMenu(id uint, req *model.Menu) error
	DeleteMenu(id uint) error
	UpdateMenuStatus(id uint, status int) error
}

// menuService 菜单服务实现
type menuService struct {
	menuRepo repository.MenuRepository
}

// NewMenuService 创建菜单服务实例
func NewMenuService(menuRepo repository.MenuRepository) MenuService {
	return &menuService{menuRepo: menuRepo}
}

// CreateMenu 创建菜单
func (s *menuService) CreateMenu(req *model.Menu) error {
	return s.menuRepo.Create(req)
}

// GetMenuByID 根据ID获取菜单
func (s *menuService) GetMenuByID(id uint) (*model.Menu, error) {
	return s.menuRepo.GetByID(id)
}

// GetMenuList 获取菜单列表
func (s *menuService) GetMenuList() ([]model.MenuTree, error) {
	return s.menuRepo.GetMenuTree()
}

// UpdateMenu 更新菜单
func (s *menuService) UpdateMenu(id uint, req *model.Menu) error {
	menu, err := s.menuRepo.GetByID(id)
	if err != nil {
		return err
	}

	// 更新菜单字段
	menu.ParentID = req.ParentID
	menu.Name = req.Name
	menu.Path = req.Path
	menu.Component = req.Component
	menu.Icon = req.Icon
	menu.Title = req.Title
	menu.Order = req.Order
	menu.Status = req.Status
	menu.Type = req.Type
	menu.Permission = req.Permission

	return s.menuRepo.Update(menu)
}

// DeleteMenu 删除菜单
func (s *menuService) DeleteMenu(id uint) error {
	return s.menuRepo.Delete(id)
}

// UpdateMenuStatus 更新菜单状态
func (s *menuService) UpdateMenuStatus(id uint, status int) error {
	return s.menuRepo.UpdateStatus(id, status)
}
